// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

/**
 * 
 */
package syscomp.entities;

import core.CodeGenerator;
import core.Entity;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.URI;

/**
 * @author riche
 *
 */
public class Mutex extends Entity {
	
	private String name;
	private String lock;
	private String unlock;
	private String wait;
	private String notify;
	private String notifyAll;
	private URI fileURI;
	private String core;
	
	protected Mutex(String name, URI fileURI, String core, String lock, String unlock, String wait, String notify, String notifyAll) {
		this.name = name;
		this.core = core;
		this.lock = lock;
		this.unlock = unlock;
		this.wait = wait;
		this.notify = notify;
		this.notifyAll= notifyAll;
		this.fileURI = fileURI;
	}
	
	public static Mutex create(String name, URI f, String c, String l, String u, String w, String n, String na) {
		return new Mutex(name, f, c, l, u, w, n, na);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the fileURI
	 */
	public URI getFileURI() {
		return fileURI;
	}

	/**
	 * @return the core
	 */
	public String getCore() {
		return core;
	}

	/**
	 * @return the lock
	 */
	public String getLock() {
		return lock;
	}

	/**
	 * @return the notify
	 */
	public String getNotify() {
		return notify;
	}

	/**
	 * @return the notifyAll
	 */
	public String getNotifyAll() {
		return notifyAll;
	}

	/**
	 * @return the unlock
	 */
	public String getUnlock() {
		return unlock;
	}

	/**
	 * @return the wait
	 */
	public String getWait() {
		return wait;
	}

	/* (non-Javadoc)
	 * @see core.Entity#generateCode()
	 */
	@Override
	public void generateCode(CodeGenerator cg) {
		String filename = "lc_" + this.getName() + "_Mutex.h";
		FileOutputStream out = null;
		PrintStream file = null;
		try {
		    out = new FileOutputStream(filename);
		    file = new PrintStream(out);
		}
		catch (FileNotFoundException e) {
		    System.err.println("Specified file " + filename + " does not exist.");
		    System.exit(1);
		}

		// Print guards
		file.println("#ifndef LC_" + this.getName().toUpperCase() + "_H_");
		file.println("#define LC_" + this.getName().toUpperCase() + "_H_");
		file.println();

		// Print standard header info
		file.println("#include <assert.h>");
		file.println("#include <lagniappe/Mutex.h>");
		file.println();
		
		file.println("#include \"" + this.getFileURI() + "\"");
		
		// Start class definition
		
		file.println("class " + this.getName() + " : public lagniappe::Mutex {");
		
		// public method definitions
		file.println("protected:");
		file.println("\t" + this.core + " coreMtx;");
		file.println("\tvirtual void lock() {coreMtx." + this.lock + "();}");
		file.println("\tvirtual void unlock() {coreMtx." + this.unlock + "();}");
		file.println("\tvirtual void wait() {coreMtx." + this.wait + "();}");
		file.println("\tvirtual void notify() {coreMtx." + this.notify + "();}");
		file.println("\tvirtual void notifyAll() {coreMtx." + this.notifyAll + "();}");
		
		// Close class definition
		
		file.println("};");
		
		// Close guards
		file.println("#endif");
		file.println();
		
	}

	/* (non-Javadoc)
	 * @see core.Entity#generateIncludes(java.io.PrintStream)
	 */
	@Override
	public void generateIncludes(PrintStream file) {
		file.println("#include \"lc_" + this.getName() + "_Mutex.h\"");
	}

}
